/**
 * 
 */
package com.nari.heatshare.service;

import java.util.Date;
import java.util.List;
import java.util.Map;

import com.nari.coreframe.service.GenericServiceImpl;
import com.nari.heatshare.dao.ITempPointDataStatisticsDao;
import com.nari.heatshare.dao.ITemperaturePointDao;
import com.nari.heatshare.dao.ITemperaturePointDataGatherDao;
import com.nari.heatshare.dao.springjdbc.ITempPointDataJdbcDao;
import com.nari.heatshare.model.ITempPointDataStatistics;
import com.nari.heatshare.model.ITemperaturePointDataGather;
import com.nari.heatshare.utils.queryvo.TempPointDataStatisExtendQuery;
import com.nari.platform.date.CalendarConstants;
import com.nari.platform.date.CalendarUtils;
import com.nari.platform.model.PageCond;
import com.nari.platform.model.ResponseMsg;
import com.nari.platform.model.ResultWithPageCond;
import com.nari.platform.utils.CommercialHighPrecisionArith;

/**
 * 
 * @author Xiaopeng
 * @date 2014-3-17
 */
public class TempPointDataStatisticsServiceImpl extends GenericServiceImpl<ITempPointDataStatistics, String> 
		implements ITempPointDataStatisticsService {
	private ITemperaturePointDao iTemperaturePointDao;
	private ITemperaturePointDataGatherDao iTemperaturePointDataGatherDao;
	private ITempPointDataJdbcDao iTempPointDataJdbcDao;
	
	/**
	 * 保存批量上传的温度点数据
	 * @param tempPointDataList
	 * @param concentratorNo
	 * @param recordTime
	 * @param dataType 数据类型，为痕迹文件生成等备用
	 * @return
	 * @throws Exception
	 * @author Xiaopeng
	 * @date 2014-12-27
	 */
	public ResponseMsg saveOrUpdateTempPointDataBatch(List<ITemperaturePointDataGather> tempPointDataList, 
			String concentratorNo, Date recordTime, Integer dataType) throws Exception {
		if(tempPointDataList!=null && tempPointDataList.size()>0) {
			int uploadCount = 0; //上传数
			//批量新增温度点数据
			for(ITemperaturePointDataGather tempPointData: tempPointDataList) {
				ResponseMsg rmsg = this.iTemperaturePointDataGatherDao.saveOrUpdateTempPointData(tempPointData);
				if(rmsg.isSuccess()) {
					uploadCount++;
				} else {
					return rmsg;
				}
			}
			//强制刷新数据到数据库，便于后续的上传率统计
			iTemperaturePointDataGatherDao.flush();
			//温度点数据的上传统计
			if(concentratorNo!=null && recordTime!=null) {
				//抄表时间格式化
				String recordTimeStr = CalendarUtils.parseDateToString(recordTime);
				//查询温度点数据上传统计信息
				ITempPointDataStatisticsDao iTempPointDataStatisticsDao = (ITempPointDataStatisticsDao) this.getiGenericDao();
				ITempPointDataStatistics uploadStatis = iTempPointDataStatisticsDao.queryUniqueStatis(concentratorNo, recordTimeStr);
				if(uploadStatis == null) {
					uploadStatis = new ITempPointDataStatistics();
					//任务批次的集中器号
					uploadStatis.setConcentratorNo(concentratorNo);
					//任务批次的抄表时间
					uploadStatis.setRecordTime(recordTimeStr);
				}
				//上传数
				uploadStatis.setUploadCount(uploadCount);
				//温度点个数
				int tempPointNum = this.iTemperaturePointDao.countTempPointsByConcentratorNo(concentratorNo);
				uploadStatis.setTempPointNum(tempPointNum);
				//温度点数据的有效上传数
				int saveSuccessCount = this.iTemperaturePointDataGatherDao.countValidUploadBatch(concentratorNo, 
						CalendarUtils.parseStringToDate(recordTimeStr, CalendarConstants.dateFormat));
				uploadStatis.setSaveSuccessCount(saveSuccessCount);
				//计算上传率
				double uploadPercent = 0;
				if(tempPointNum > 0) {
					uploadPercent = CommercialHighPrecisionArith.divide(saveSuccessCount*100, tempPointNum, 5);
				}
				uploadStatis.setUploadPercent(uploadPercent);
				//最后更新时间
				uploadStatis.setUpdateTime(new Date());
				this.getiGenericDao().saveOrUpdateEntity(uploadStatis);
			}
		}
		return ResponseMsg.getInstance(true, "");
	}
	
	/**
	 * 分页查询温度点数据统计信息
	 * @param concentratorNo
	 * @param recordTime
	 * @param pageCond
	 * @return
	 * @throws Exception
	 * @author Xiaopeng
	 * @date 2014-3-23
	 */
	public ResultWithPageCond<ITempPointDataStatistics> queryStatisPage(String concentratorNo, 
			String recordTime, PageCond pageCond) throws Exception {
		ITempPointDataStatisticsDao iTempPointDataStatisticsDao = (ITempPointDataStatisticsDao) this.getiGenericDao();
		return iTempPointDataStatisticsDao.queryStatisPage(concentratorNo, recordTime, pageCond);
	}
	
	/**
	 * 删除温度点数据批次任务信息
	 * @param tpDataStatisUID
	 * @return
	 * @throws Exception
	 * @author Xiaopeng
	 * @date 2014-4-9
	 */
	public ResponseMsg deleteTempPointDataBatch(String tpDataStatisUID) throws Exception {
		//查询温度点数据统计信息
		ITempPointDataStatistics tempPointDataStatis = this.getiGenericDao().queryEntityById(tpDataStatisUID);
		if(tempPointDataStatis != null) {
			//删除温度点数据批次
			Date recordTime = CalendarUtils.parseStringToDate(tempPointDataStatis.getRecordTime(), CalendarConstants.dateFormat);
			iTemperaturePointDataGatherDao.deleteTempPointDataBatch(tempPointDataStatis.getConcentratorNo(), recordTime);
			//删除温度点数据统计信息
			this.getiGenericDao().deleteEntityById(tpDataStatisUID);
		}
		return ResponseMsg.getInstance(true, "");
	}
	
	/**
	 * 根据温度点数据批次列表查询其对应的档案资料扩展信息
	 * @param tpdStatisList
	 * @return
	 * @throws Exception
	 * @author Xiaopeng
	 * @date 2015-1-25
	 */
	public Map<String, TempPointDataStatisExtendQuery> queryTpdStatisExtend(List<ITempPointDataStatistics> 
			tpdStatisList) throws Exception {
		StringBuffer meterDataStatisUIDList = new StringBuffer("");
		if(tpdStatisList!=null && tpdStatisList.size()>0) {
			for(int index=0; index<tpdStatisList.size(); index++) {
				meterDataStatisUIDList.append(tpdStatisList.get(index).getId());
				if(index < tpdStatisList.size()-1) {
					meterDataStatisUIDList.append(",");
				}
			}
		}
		return iTempPointDataJdbcDao.queryTpdStatisExtend(meterDataStatisUIDList.toString());
	}

	/**
	 * @return the iTemperaturePointDao
	 */
	public ITemperaturePointDao getiTemperaturePointDao() {
		return iTemperaturePointDao;
	}

	/**
	 * @param iTemperaturePointDao the iTemperaturePointDao to set
	 */
	public void setiTemperaturePointDao(ITemperaturePointDao iTemperaturePointDao) {
		this.iTemperaturePointDao = iTemperaturePointDao;
	}

	/**
	 * @return the iTemperaturePointDataGatherDao
	 */
	public ITemperaturePointDataGatherDao getiTemperaturePointDataGatherDao() {
		return iTemperaturePointDataGatherDao;
	}

	/**
	 * @param iTemperaturePointDataGatherDao the iTemperaturePointDataGatherDao to set
	 */
	public void setiTemperaturePointDataGatherDao(
			ITemperaturePointDataGatherDao iTemperaturePointDataGatherDao) {
		this.iTemperaturePointDataGatherDao = iTemperaturePointDataGatherDao;
	}

	/**
	 * @return the iTempPointDataJdbcDao
	 */
	public ITempPointDataJdbcDao getiTempPointDataJdbcDao() {
		return iTempPointDataJdbcDao;
	}

	/**
	 * @param iTempPointDataJdbcDao the iTempPointDataJdbcDao to set
	 */
	public void setiTempPointDataJdbcDao(ITempPointDataJdbcDao iTempPointDataJdbcDao) {
		this.iTempPointDataJdbcDao = iTempPointDataJdbcDao;
	}
	
}
